package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.InventoryDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;

import com.atguigu.jxc.service.InventoryService;
import com.github.pagehelper.PageHelper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class InventoryServiceImpl implements InventoryService {
    @Autowired
    private InventoryDao inventoryDao;
    @Override
    public Map<String, Object> selectInventoryPage(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {

        Map<String, Object> map = new HashMap<>();

     Integer total = inventoryDao.selectInventoryCount(codeOrName,goodsTypeId);
       PageHelper.startPage(page,rows);
        List<Goods> goods = inventoryDao.getGoodList(codeOrName,goodsTypeId);
        for (Goods good : goods) {
            if(good.getSaleTotal() == null) {
                good.setSaleTotal(0);
            }
            continue;
        }

     map.put("total", total);
        map.put("rows", goods);

        return map;
    }

    @Override
    public Map<String, Object> getNoInventoryQuantity(Integer page, Integer rows,
                                                      String nameOrCode) {
        Map<String, Object> map = new HashMap<>();

        PageHelper.startPage(page,rows);
        List<Goods> goods = inventoryDao.getInventoryGoodList(nameOrCode);
        List<Goods> collect = goods.stream().filter(goods1 -> goods1.getInventoryQuantity() <= 0).collect(Collectors.toList());
        for (Goods good : collect) {
            if (good.getSaleTotal() == null) {
                good.setSaleTotal(0);
            }
            continue;
        }
        Integer total = collect.size();
        map.put("total", total);
        map.put("rows", collect);

        return map;
    }

    @Override
    public Map<String, Object> getHasInventoryQuantity(Integer page, Integer rows,
                                                       String nameOrCode) {
        Map<String, Object> map = new HashMap<>();

        PageHelper.startPage(page,rows);
        List<Goods> goods = inventoryDao.getInventoryGoodList(nameOrCode);
        List<Goods> collect = goods.stream().filter(goods1 -> goods1.getInventoryQuantity() > 0).collect(Collectors.toList());
        for (Goods good : collect) {
            if (good.getSaleTotal() == null) {
                good.setSaleTotal(0);
            }
            continue;
        }
        Integer total = collect.size();
        map.put("total", total);
        map.put("rows", collect);

        return map;
    }

    @Override
    public ServiceVO saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        inventoryDao.saveStock(goodsId,inventoryQuantity,purchasingPrice);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    @Override
    public ServiceVO deleteStock(Integer goodsId) {
        Goods goods = inventoryDao.selectInventory(goodsId);
        if(goods.getState() == 1 || goods.getState() == 2){
            return new ServiceVO(ErrorCode.STORED_ERROR_CODE,ErrorCode.STORED_ERROR_MESS);
        }
            inventoryDao.deleteStock(goodsId);
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);

    }

}
